home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / DIZZY / SRC / QUICKRAW.C < prev    next >
C/C++ Source or Header  |  1990-12-29  |  8KB  |  391 lines

  1. #include "dizzy.h"
  2. #ifndef MACINTOSH
  3. #include "xstuff.h"
  4.  
  5. Pixmap    gridpix;
  6.  
  7. static    int     CurrentPenMode;
  8. static    char    GrayData[]={0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA};
  9. static    Pixmap    graypix;
  10.  
  11. void    FrameRect(r)
  12. Rect    *r;
  13. {
  14.     if(NonEmpty(r))
  15.         XDrawRectangle(Disp,Wind,Mode,
  16.                         orig_x+(r)->left,
  17.                         orig_y+(r)->top,
  18.                         (r)->right-(r)->left-1,(r)->bottom-(r)->top-1);
  19. }
  20. void    PaintRect(r)
  21. Rect    *r;
  22. {
  23.     if(NonEmpty(r))
  24.         XFillRectangle(Disp,Wind,Mode,
  25.                         orig_x+(r)->left,
  26.                         orig_y+(r)->top,
  27.                         (r)->right-(r)->left,(r)->bottom-(r)->top);
  28. }
  29.  
  30. void    SetRect(r,x1,y1,x2,y2)
  31. Rect    *r;
  32. int     x1,y1,x2,y2;
  33. {
  34.     r->left=x1;
  35.     r->right=x2;
  36.     r->top=y1;
  37.     r->bottom=y2;    
  38. }
  39.  
  40. void    EraseRect(r)
  41. Rect    *r;
  42. {
  43.     XSetForeground(Disp,Mode,White);
  44.     PaintRect(r);
  45.     XSetForeground(Disp,Mode,Black);
  46. }
  47.  
  48. void    InvertRect(r)
  49. Rect    *r;
  50. {
  51.     XSetFunction(Disp,Mode,GXxor);
  52.     XSetForeground(Disp,Mode,Black ^ White);
  53.     PaintRect(r);
  54.     XSetFunction(Disp,Mode,CurrentPenMode);
  55.     XSetForeground(Disp,Mode,Black);
  56. }
  57.  
  58. void    LineTo(x,y)
  59. int     x,y;
  60. {
  61.     XDrawLine(Disp,Wind,Mode,macpen_x+orig_x,macpen_y+orig_y,x+orig_x,y+orig_y);
  62.     MoveTo(x,y);
  63. }
  64. void    Line(x,y)
  65. int     x,y;
  66. {
  67.     x+= macpen_x;
  68.     y+= macpen_y;
  69.     XDrawLine(Disp,Wind,Mode,macpen_x+orig_x,macpen_y+orig_y,x+orig_x,y+orig_y);
  70.     MoveTo(x,y);
  71. }
  72.  
  73. void    InsetRect(r,h,v)
  74. Rect    *r;
  75. int     h,v;
  76. {
  77.     r->left+=h;
  78.     r->right-=h;
  79.     r->top+=v;
  80.     r->bottom-=v;
  81. }
  82.  
  83. void    OffsetRect(r,h,v)
  84. Rect    *r;
  85. int     h,v;
  86. {
  87.     r->left+=h;
  88.     r->right+=h;
  89.     r->top+=v;
  90.     r->bottom+=v;
  91. }
  92.  
  93. int     PtInRect(pt,r)
  94. Point    pt;
  95. Rect    *r;
  96. {
  97.     if(r->left>pt.h)    return FALSE;
  98.     if(r->right<=pt.h)    return FALSE;
  99.     if(r->top>pt.v)     return FALSE;
  100.     if(r->bottom<=pt.v) return FALSE;
  101.     return TRUE;
  102. }
  103.  
  104. void    DrawText(s,start,len)
  105. char    *s;
  106. long    start;
  107. long    len;
  108. {
  109.     XDrawString(Disp,Wind,Mode,macpen_x+orig_x,macpen_y+orig_y,s+start,len);
  110.     macpen_x+=XTextWidth(MyFont,s+start,len);
  111. }
  112.  
  113. void    DrawChar(c)
  114. char    c;
  115. {
  116.     XDrawString(Disp,Wind,Mode,macpen_x+orig_x,macpen_y+orig_y,&c,1);
  117.     macpen_x+=XTextWidth(MyFont,&c,1);
  118. }
  119.  
  120. int     TextWidth(s,start,len)
  121. char    *s;
  122. long    start;
  123. long    len;
  124. {
  125.     return    XTextWidth(MyFont,s+start,len);
  126. }
  127. /*
  128. >>    Mark a rectangular area as invalid. Causes an expose event
  129. >>    on that area.
  130. */
  131. void    InvalRect(r)
  132. Rect    *r;
  133. {
  134.     if(NonEmpty(r))
  135.     {    XClearArea(Disp,Wind, r->left+orig_x, r->top+orig_y,
  136.                           r->right-r->left, r->bottom-r->top, TRUE);
  137.     }
  138. }
  139.  
  140. /*
  141. >>    Create a pixmap from bitmap data.
  142. */
  143. Pixmap    CreatePixMap(bits,width,height)
  144. char    *bits;
  145. int     width,height;
  146. {
  147.     Pixmap    bitsis,pixis;
  148.     
  149.     bitsis=XCreateBitmapFromData(Disp,RootWindowOfScreen(XtScreen(Canvas)),
  150.                                  bits,width,height);
  151.     pixis=XCreatePixmap(Disp,RootWindowOfScreen(XtScreen(Canvas)),
  152.                         width,height,DefaultDepthOfScreen(XtScreen(Canvas)));
  153.     XCopyPlane(Disp,bitsis,pixis,Mode,0,0,width,height,0,0,1);
  154.     XFreePixmap(Disp,bitsis);
  155.     
  156.     return pixis;
  157. }
  158.  
  159. void    SetOrigin(x,y)
  160. int     x,y;
  161. {
  162.     orig_x= -x;
  163.     orig_y= -y;
  164. }
  165.  
  166. void    PenSize(m,n)
  167. int     m,n;
  168. {
  169.     if(m==1) m=0;    /*    Use HW line drawing, if possible.    */
  170.     XSetLineAttributes(Disp,Mode,m,LineSolid,CapButt,JoinMiter);
  171. }
  172.  
  173. void    PenXor()
  174. {
  175.     CurrentPenMode=GXxor;
  176.     XSetFunction(Disp,Mode,GXxor);
  177. }
  178. void    PenCopy()
  179. {
  180.     CurrentPenMode=GXcopy;
  181.     XSetFunction(Disp,Mode,GXcopy);
  182. }
  183.  
  184. /*
  185. >>    Copy a pixmap portion to our window.
  186. */
  187. void    PixCopy(pixels,srcx,srcy,dest)
  188. Pixmap    pixels;
  189. int     srcx,srcy;
  190. Rect    *dest;
  191. {
  192.     XCopyPlane(Disp,pixels,Wind,Mode,srcx,srcy,
  193.                 dest->right-dest->left,
  194.                 dest->bottom-dest->top,
  195.                 dest->left+orig_x,dest->top+orig_y,1);
  196. }
  197. void    MiscPixCopy(srcx,srcy,dest)
  198. int     srcx,srcy;
  199. Rect    *dest;
  200. {
  201.     PixCopy(MiscPix,srcx,srcy,dest);
  202. }
  203. /*
  204. >>    Find out last position of the mouse cursor.
  205. */
  206. void    GetMouseDownPoint(pt)
  207. Point    *pt;
  208. {
  209.     pt->h=MyEvent.xbutton.x-orig_x;
  210.     pt->v=MyEvent.xbutton.y-orig_y;
  211. }
  212. /*
  213. >>    Find out which button is/was down.
  214. */
  215. int     GetDownButton()
  216. {
  217.     switch(MyEvent.xbutton.button)
  218.     {    case Button1:    return 0;
  219.         case Button2:    return 1;
  220.         case Button3:    return 2;
  221.         default:        return 0;
  222.     }
  223. }
  224.  
  225.  
  226. /*
  227. >>    Gets the mouse position while in a tracking loop.
  228. >>    Returns true when the mouse button comes up.
  229. */
  230. int GetMouseTrackEvent(pt)
  231. Point    *pt;
  232. {
  233.     int     waitflag;
  234.     
  235.     if(DownTrigger)
  236.     {    GetMouseDownPoint(pt);
  237.         DownTrigger=FALSE;
  238.         return    TRUE;
  239.     }
  240.  
  241.     XMaskEvent(Disp, ButtonPressMask | ButtonMotionMask | ButtonReleaseMask, &MyEvent);
  242.     waitflag= (MyEvent.type==MotionNotify);
  243.     while(waitflag)
  244.     {    if(XCheckMaskEvent(Disp,ButtonPressMask | ButtonMotionMask |
  245.                              ButtonReleaseMask, &MyEvent))
  246.         {    waitflag= (MyEvent.type==MotionNotify);
  247.         }
  248.         else
  249.         {    waitflag= 0;
  250.         }
  251.     }
  252.     GetMouseDownPoint(pt);
  253.  
  254.     if(MyEvent.type==ButtonRelease)
  255.         return    FALSE;
  256.     return    TRUE;
  257. }
  258. /*
  259. >>    Use a rectangle for clipping. The rectangle is in local coordinates.
  260. */
  261. void    ClipRect(r)
  262. Rect    *r;
  263. {
  264.     XRectangle    Clipper;
  265.     
  266.     Clipper.x=r->left+orig_x;
  267.     Clipper.y=r->top+orig_y;
  268.     Clipper.width=r->right-r->left;
  269.     Clipper.height=r->bottom-r->top;
  270.     if(Clipper.width<0) Clipper.width=0;
  271.     if(Clipper.height<0) Clipper.height=0;
  272.     
  273.     XSetClipRectangles(Disp,Mode,0,0,&Clipper,1,Unsorted);
  274. }
  275. /*
  276. >>    Using a stippled pen to draw rectangle outlines and
  277. >>    lines didn't work with MacX and was slow on Sony
  278. >>    News workstations, so I just replaced it with a solid
  279. >>    black. It looks ok.
  280. */
  281. void    PenGray()    
  282. {
  283.     XSetFillStyle(Disp,Mode,FillSolid);
  284. }
  285. /*
  286. >>    The background still needs to be drawn with a
  287. >>    gray tiled pattern, so I created this mode for
  288. >>    just that purpose. Filling rectangles with a
  289. >>    tile or stipple worked ok even with MacX.
  290. */
  291. void    PenBGray()
  292. {
  293.     XSetFillStyle(Disp,Mode,FillTiled);
  294.     XSetTile(Disp,Mode,graypix);
  295.     XSetTSOrigin(Disp,Mode,0,0);
  296. }
  297. /*
  298. >>    This tile pattern draws a grid with 64 pixels
  299. >>    between each gridline and lines with just one
  300. >>    pixel out of four drawn. Looks nice.
  301. */
  302. void    PenGrid()
  303. {
  304.     XSetFillStyle(Disp,Mode,FillTiled);
  305.     XSetTile(Disp,Mode,gridpix);
  306.     XSetTSOrigin(Disp,Mode,orig_x,orig_y);
  307. }
  308.  
  309. /*
  310. >>    Just the dull black pen to draw most of
  311. >>    the things that need to be drawn. We assume
  312. >>    that the foreground color is already correctly
  313. >>    set.
  314. */
  315. void    PenBlack()
  316. {
  317.     XSetFillStyle(Disp,Mode,FillSolid);
  318. }
  319. void    InitXGraf()
  320. {    
  321.     int     i;
  322.     
  323.     SetRect(&NilRect,0,0,0,0);
  324.     Wind=XtWindow(Canvas);
  325.     Disp=XtDisplay(Canvas);
  326.  
  327.     graypix=CreatePixMap(GrayData,8,8);
  328.     gridpix=XCreatePixmap(Disp,RootWindowOfScreen(XtScreen(Canvas)),
  329.                         64,64,DefaultDepthOfScreen(XtScreen(Canvas)));
  330.     
  331.     XSetForeground(Disp,Mode,WhitePixel(Disp,DefaultScreen(Disp)));
  332.     XFillRectangle(Disp,gridpix,Mode,0,0,64,64);
  333.     XSetForeground(Disp,Mode,BlackPixel(Disp,DefaultScreen(Disp)));
  334.  
  335.  
  336.     for(i=2;i<64;i+=4)
  337.     {    XDrawPoint(Disp,gridpix,Mode,i,0);
  338.         XDrawPoint(Disp,gridpix,Mode,0,i);
  339.     }
  340. }
  341.  
  342. /*
  343. >>    This routines handles the scrolling of the drawing area.
  344. >>    It doesn't work quite as nicely as the Macintosh version,
  345. >>    but the basic idea is the same.
  346. */
  347. void    DoHandScroller()
  348. {
  349.     Rect        dest;
  350.     Pixmap        temp;
  351.     int         dh,dv;
  352.     Point        StartSpot,OldSpot,MousePoint;
  353.     int         downflag;
  354.  
  355.     ClipEditArea();
  356.     
  357.     GetMouseDownPoint(&StartSpot);
  358.     OldSpot=StartSpot;
  359.  
  360.     do
  361.     {    downflag=GetMouseTrackEvent(&MousePoint);
  362.         
  363.         if(MousePoint.h!=OldSpot.h || MousePoint.v != OldSpot.v)
  364.         {    dest=EditClipper;
  365.             dh=MousePoint.h-OldSpot.h;
  366.             dv=MousePoint.v-OldSpot.v;
  367.             OffsetRect(&dest,dh,dv);
  368.             PixCopy(Wind,EditR.left,EditR.top,&dest);
  369.  
  370.             dest=EditClipper;
  371.             if(dh<0)    dest.left=dest.right+dh;
  372.             else        dest.right=dest.left+dh;
  373.             EraseRect(&dest);
  374.  
  375.             dest=EditClipper;
  376.             if(dv<0)    dest.top=dest.bottom+dv;
  377.             else        dest.bottom=dest.top+dv;
  378.             EraseRect(&dest);
  379.             
  380.             OldSpot=MousePoint;
  381.         }
  382.     }    while(downflag);
  383.     
  384.     CurHeader->XOrig-=OldSpot.h-StartSpot.h;
  385.     CurHeader->YOrig-=OldSpot.v-StartSpot.v;
  386.     
  387.     InvalRect(&EditClipper);    /*    Redraw everything.    */
  388.     RestoreClipping();
  389. }
  390. #endif
  391.